#include "list.h"
List::List()
{
    this->size = 0;
    this->capacity = 0;
    this->data = new byte[INITIAL_SIZE];
}

List::~List()
{
    this->clearMemory();
}

byte * List::getPointerData()
{
    return this->data;
}

bool List::insert(byte data)
{
    if (this->capacity == this->size){
        if ( !this->reallocMemory())
            return false;
    }
    
    this->data[this->size++] = data;

    return true;
}

void List::clear()
{
    this->size = 0;
}

void List::clearMemory()
{
    if (this->data != NULL)
        delete this->data;

    this->size = this->capacity = 0;
    this->data = NULL;
}

void List::copy(const byte *from, int size)
{
    for (int i = 0; i < size; i++)
        this->insert(from[i]);
}

bool List::reallocMemory()
{
    unsigned long int nSize;

    if (this->capacity > 0){
        nSize = this->capacity*2;
        this->capacity *= 2;
    } else {
        nSize = INITIAL_SIZE;
        this->capacity = INITIAL_SIZE;
    }

    if ( !(this->data = (byte *)realloc(this->data, nSize)))
        return false;

    return true;
}

unsigned long int List::getSize()
{
    return this->size;
}

void List::showList(bool type)
{
    unsigned long int i;
    ofstream file (".tmpList");
    
    for (i = 0; i < this->size; i++)
        file << this->getPointerData()[i];
    file.close();

    if (type)
        system("more .tmpList");
    else
        system("cat .tmpList");

    remove(".tmpList");
}
